/**
 *
 */
package com.edooschool.webpay.entity.callback;

import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.demo2do.core.wechat.entity.pay.PayConfig;
import com.demo2do.core.wechat.entity.pay.PayTransaction;
import com.edooschool.webpay.entity.PayChannel;
import com.edooschool.webpay.entity.config.WechatpayConfig;

/**
 * @author downpour
 *
 */
public class WechatpayCallbackRequest implements CallbackRequest {

    private PayConfig payConfig;
    
    private PayTransaction payTransaction;
    
    private Map<String, String> requestParameters;
    
    /**
     * @param requestParameters
     */
    public WechatpayCallbackRequest(Map<String, String> requestParameters) {
        
        // init request_parameters
        this.requestParameters = requestParameters;
        
        // init pay_config
        this.payConfig = initPayConfig();
        
        // init pay_transaction
        this.payTransaction = initPayTransaction();
        
    }
    
    /**
     * 
     * @return
     */
    private PayConfig initPayConfig() {
        
        // get params from request_parameters
        String appid = this.requestParameters.get("appid");
        String partnerId = this.requestParameters.get("mch_id");
        
        // construct wechatpay config
        return new WechatpayConfig(appid, partnerId);
    }
    
    /**
     * 
     * @return
     */
    private PayTransaction initPayTransaction() {
        
        String outTradeNo = requestParameters.get("out_trade_no");
        String transactionCode = requestParameters.get("transaction_id");
        String amount = requestParameters.get("total_fee");
        String paymentTime = requestParameters.get("time_end");
        
        return new CallbackContext(outTradeNo, transactionCode, amount, paymentTime);
    }

    /* (non-Javadoc)
     * @see com.edooschool.webpay.entity.callback.CallbackRequest#getRequestParameters()
     */
    public Map<String, String> getRequestParameters() {
        return requestParameters;
    }
    
    /* (non-Javadoc)
     * @see com.edooschool.webpay.entity.callback.CallbackRequest#getPayChannel()
     */
    public PayChannel getPayChannel() {
        return PayChannel.WECHATPAY;
    }

    /* (non-Javadoc)
     * @see com.edooschool.webpay.entity.callback.CallbackRequest#getPayConfig()
     */
    public PayConfig getPayConfig() {
        return this.payConfig;
    }
    
    /* (non-Javadoc)
     * @see com.edooschool.webpay.entity.callback.CallbackRequest#getPayTransaction()
     */
    public PayTransaction getPayTransaction() {
        return this.payTransaction;
    }

    /* (non-Javadoc)
     * @see com.edooschool.webpay.entity.callback.CallbackRequest#isValid()
     */
    public boolean isValid() {
        return StringUtils.isNotBlank(this.payTransaction.getOutTradeNo());
    }
    
    /* (non-Javadoc)
     * @see com.edooschool.webpay.entity.callback.CallbackRequest#getSign()
     */
    public String getSign() {
        return requestParameters.get("sign");
    }
	
}
